package com.lkd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.mapper.VendingMachineMapper;
import com.lkd.result.PageBean;
import com.lkd.service.*;
import com.lkd.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VendingMachineServiceImpl implements VendingMachineService {
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private NodeService nodeService;
    @Autowired
    private RegionService regionService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private VmTypeService vmTypeService;

    @Override
    public void deleteByPartnerId(Integer id) {
        vendingMachineMapper.deleteByPartnerId(id);
    }

    @Override
    public List<VendingMachineVo> selectByNodeId(Long nodeId) {
        return vendingMachineMapper.selectByNodeId(nodeId);
    }

    @Override
    public PageBean<VendingMachineVo> findByPage(Integer pageIndex, Integer pageSize, String innerCode) {

        PageHelper.startPage(pageIndex,pageSize);
        List<VendingMachineVo> voList = vendingMachineMapper.selectByPage(innerCode);

        VmTypeVo vmTypeVo = vmTypeService.selectByInnerCode(innerCode);

        NodeVo nodeVo = nodeService.selectByInnerCode(innerCode);
        RegionVo regionVo = regionService.selectRegionByInnerCode(innerCode);
        BusinessVo businessVo = businessService.selectBusinessByInnerCode(innerCode);
        nodeVo.setRegion(regionVo);
        nodeVo.setBusinessType(businessVo);

        for (VendingMachineVo vo : voList) {
            vo.setType(vmTypeVo);
            vo.setNode(nodeVo);
            vo.setRegion(regionVo);
        }
        Page page = (Page) voList;
        return new PageBean(pageIndex, pageSize, (long) page.getPages(), page.getTotal(), page.getResult());
    }
}